﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.Media;
using EnvDTE;

namespace HangedManConsulting.PomodoroTimerforVS
{
    /// <summary>
    /// Interaction logic for PomodoroTimer.xaml
    /// </summary>
    public partial class PomodoroTimer : UserControl
    {
        //private const int POMODORO_LENGTH = 25;
        //private const int SHORT_BREAK_LENGTH = 5;
        //private const int LONG_BREAK_LENGTH = 15;
        //private const int POMODOROS_PER_LONG_BREAK = 4;
        //private const bool USE_AUDIO_FEEDBACK_FOR_TIMER_END = true;
        //private const bool FLASH_TASKBAR_FOR_TIMER_END = true;

        public int PomodoroLength { get; set; }
        public int ShortBreakLength { get; set; }
        public int LongBreakLength { get; set; }
        public int PomodorosPerCycle { get; set; }
        public bool PlayAudioFeedbackForTimerEnd { get; set; }
        public bool FlashTaskbarIconForTimerEnd { get; set; }

        private TimeSpan remainingTime;
        private PomodoroState state;
        private int pomodorosThisCycle;

        public PomodoroWindow ThisParent { get; set; }

        private DispatcherTimer timer;


        public PomodoroTimer(PomodoroWindow _parent)
        {
            InitializeComponent();

            GetUserOptions();
            State.Text = state.ToString();
            PomodoroCount.Text = pomodorosThisCycle.ToString();

#if DEBUG
            State.Visibility = Visibility.Visible;
            PomodoroCount.Visibility = Visibility.Visible;
#endif

            //ThisParent = _parent;

            ResetTimer(new TimeSpan(0, PomodoroTimerforVSPackage.PomodoroLength, 0));

            var _dte = PomodoroTimerforVSPackage.GetGlobalService(typeof(DTE)) as DTE;

        }

        private void GetUserOptions()
        {

            PomodoroLength = PomodoroTimerforVSPackage.PomodoroLength;
            ShortBreakLength = PomodoroTimerforVSPackage.ShortBreakLength;
            LongBreakLength = PomodoroTimerforVSPackage.LongBreakLength;
            PomodorosPerCycle = PomodoroTimerforVSPackage.PomodorosPerCycle;
            PlayAudioFeedbackForTimerEnd = PomodoroTimerforVSPackage.PlayAudioFeedbackForTimerEnd;
            FlashTaskbarIconForTimerEnd = PomodoroTimerforVSPackage.FlashTaskbarIconForTimerEnd;

        }

        void timer_Tick(object sender, EventArgs e)
        {

            remainingTime = remainingTime.Subtract(timer.Interval);
            if (remainingTime.Seconds < 0 ) {
                HandeTimerEnd();
            }
            Counter.Foreground = GetTextBrush(remainingTime.Minutes);
            Counter.Text = remainingTime.ToString(@"mm\:ss");

        }

        private void HandeTimerEnd()
        {

            timer.Stop();

             if (PlayAudioFeedbackForTimerEnd) {
                 PomodoroTimerforVSPackage.ring();
             }

             if (FlashTaskbarIconForTimerEnd) {
                 PomodoroTimerforVSPackage.FlashUntilFocussed();
             }

            switch (this.state) {
                case PomodoroState.ShortBreak:
                    this.state = PomodoroState.Pomodoro;
                    ResetTimer(new TimeSpan(0, PomodoroLength, 0));
                    break;
                case PomodoroState.LongBreak:
                    this.pomodorosThisCycle = 0;
                    this.state = PomodoroState.Pomodoro;
                    ResetTimer(new TimeSpan(0, PomodoroLength, 0));
                    break;
                case PomodoroState.Pomodoro:
                default:
                    this.pomodorosThisCycle += 1;
                    if (pomodorosThisCycle == PomodorosPerCycle) {
                        this.state = PomodoroState.LongBreak;
                        ResetTimer(new TimeSpan(0, LongBreakLength, 0));
                    } else {
                        this.state = PomodoroState.ShortBreak;
                        ResetTimer(new TimeSpan(0, ShortBreakLength, 0));
                    }
                    break;
            }

            State.Text = state.ToString();
            PomodoroCount.Text = pomodorosThisCycle.ToString();

        }

        private SolidColorBrush GetTextBrush(int remainingMinutes)
        {
            if (remainingMinutes < 5 && this.state == PomodoroState.Pomodoro) {
                return new SolidColorBrush {
                    Color = Colors.Red
                };

            } else {
                return new SolidColorBrush {
                    Color = Colors.Black
                };
            }
        }

        private void ResetTimer(TimeSpan resetTimespan)
        {
            State.Text = state.ToString();

            remainingTime = resetTimespan;
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(10000000);
            timer.Tick += new EventHandler(timer_Tick);
            Counter.Text = remainingTime.ToString(@"mm\:ss");
            stopPomodoro.Content = "Stop";
        }

        private void startPomodoro_Click(object sender, RoutedEventArgs e)
        {

            GetUserOptions();

            Counter.Text = remainingTime.ToString(@"mm\:ss");
            stopPomodoro.Content = "Stop";
            timer.Start();

        }

        private void stopPomodoro_Click(object sender, RoutedEventArgs e)
        {
            GetUserOptions();

            if (timer.IsEnabled) {
                timer.Stop();
                stopPomodoro.Content = "Reset";
            } else {
                ResetTimer(new TimeSpan(0, PomodoroLength, 0));
            }

        }

    }

    public enum PomodoroState
    {

        Pomodoro,
        ShortBreak,
        LongBreak
        
    }
}